﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using Infragistics.Controls.Grids;
using StartupSolution.ViewModel;

namespace StartupSolution.Views.Grids
{
    public partial class ReadOnlyGrid : Page
    {
        public ReadOnlyGrid()
        {
            InitializeComponent();

            this.Loaded += new System.Windows.RoutedEventHandler(ReadOnlyGrid_Loaded);
        }

        private void ReadOnlyGrid_Loaded(object sender, RoutedEventArgs e)
        {
            /// <summary> 
            /// This method will load data from a XML document and populate it as XamGrid ItemSource
            /// </summary>
            LoadXMLData();
        }

        private void LoadXMLData()
        {
            // 	Loads the XML file and creates a XDocument object
            XDocument doc = XDocument.Load("Datasource/Products.xml");

            // Run LINQ query to retrieve the necessary data
            var data = (from d in doc.Descendants("Products")
								  select new Product
								  {
                                      // Gets ProductID field
									  SKU = d.Element("ProductID").Value,
                                      // Gets ProductName field
									  Name = d.Element("ProductName").Value,
                                      // Gets Category field 
									  Category = d.Element("Category").Value,
                                      // Gets Supplier field 
									  Supplier = d.Element("Supplier").Value,
                                      // Gets QuantityPerUnit field
									  QuantityPerUnit = d.Element("QuantityPerUnit").Value
                                      // TODO: Retrieve all the fields from XDocument and fill the XamGrid ItemSource
								  }).ToList<Product>();

            // Set the ItemsSource to generate the columns.
            igGrid.ItemsSource = data;

            // TODO: Enable the sorting in the XamGrid control
            // Sorting
            // This code is presented in the XAML page.
            // If you want to implement the sorting functionality in the code-behind, comment the sorting part in the XAML page.
            //
            // igGrid.SortingSettings.AllowSorting = true;
            // igGrid.SortingSettings.AllowMultipleColumnSorting = true;
            // igGrid.SortingSettings.ShowSortIndicator = true;

            // TODO: Enable the filtering in the XamGrid control
            // Filtering
            // This code is presented in the XAML page.
            // If you want to implement the filtering functionality in the code-behind, comment the filtering part in the XAML page.
            //
            // igGrid.FilteringSettings.AllowFiltering = FilterUIType.FilterRowTop;

            // TODO: Enable the Group By in the XamGrid control
            // Grouping
            // This code is presented in the XAML page.
            // If you want to implement the group-by functionality in the code-behind, comment the group-by part in the XAML page.
            //
            // igGrid.GroupBySettings.AllowGroupByArea = GroupByAreaLocation.Top;

            // TODO: Enable Cell Merging in the XamGrid control
            // Cell Merging
            // This code is presented in the XAML page.
            // If you want to implement the cell merging functionality in the code-behind, comment the group-by part in the XAML page.
            //
            // MyDataGrid.GroupBySettings.GroupByOperation = GroupByOperation.MergeCells;


            // TODO: Enable the Paging in the XamGrid control
            // Paging
            // This code is presented in the XAML page.
            // If you want to implement the paging in the code-behind, comment the paging part in the XAML page.
            //
            // igGrid.PagerSettings.AllowPaging = PagingLocation.Bottom;
            // igGrid.PagerSettings.PageSize = 10;

        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
        }

    }
}
